package com.hj.utils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import java.io.File;
import java.io.UnsupportedEncodingException;
import java.util.Map;

/**
 * @author Oakhole
 * @since 1.0
 */
@Component
public class MailHelper {

    Logger logger = LoggerFactory.getLogger(MailHelper.class);

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${mail.username}")
    private String from;

    /**
     * 通过application.properties注入值，用于向应用管理员发出告警信息
     */
    @Value("${mail.toUser}")
    private String toUser;

    @Value("${mail.nickname}")
    private String mail_nickname;

    /**
     * 带附件邮件发送
     *
     * @param toUser
     * @param subject
     * @param content
     * @param attachmentName
     * @param attachment
     * @return
     * @throws MessagingException
     * @throws UnsupportedEncodingException
     */
    public boolean mailto(String toUser, String subject, String content, String attachmentName, File attachment) {
        MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(new InternetAddress(MimeUtility.encodeText(mail_nickname) + "<" + from + ">"));
            mimeMessageHelper.setTo(StringUtils.isEmpty(toUser) ? this.toUser : toUser);
            mimeMessage.setSubject(subject);
            mimeMessageHelper.setText(content, false);
            mimeMessageHelper.addAttachment(attachmentName, attachment);
            this.javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return true;
        } finally {
            return true;
        }
    }

    /**
     * 发送多附件邮件
     *
     * @param toUser
     * @param subject
     * @param content
     * @param multi_attachment
     * @return
     */
    public boolean mailto(String toUser, String subject, String content, Map<String, String> multi_attachment) {

        MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(new InternetAddress(MimeUtility.encodeText(mail_nickname) + "<" + from + ">"));
            mimeMessageHelper.setTo(StringUtils.isEmpty(toUser) ? this.toUser : toUser);
            mimeMessage.setSubject(subject);
            mimeMessageHelper.setBcc(new String[]{"18652023713@163.com"});
            mimeMessageHelper.setText(content, true);
            for (Map.Entry<String, String> entry : multi_attachment.entrySet()) {
                mimeMessageHelper.addAttachment(entry.getKey(), new File(entry.getValue()));
            }
            this.javaMailSender.send(mimeMessage);
        } catch (Exception e) {
            e.printStackTrace();
            logger.error(e.getStackTrace().toString());
            return false;
        }

        return true;
    }

    /**
     * 发送普通文本邮件
     *
     * @param toUser
     * @param subject
     * @param content
     * @return
     * @throws MessagingException
     */
    public boolean mailto(String toUser, String subject, String content) {
        MimeMessage mimeMessage = this.javaMailSender.createMimeMessage();
        try {
            MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(mimeMessage, true);
            mimeMessageHelper.setFrom(new InternetAddress(MimeUtility.encodeText(mail_nickname) + "<" + from + ">"));
            mimeMessageHelper.setTo(StringUtils.isEmpty(toUser) ? this.toUser : toUser);
            mimeMessage.setSubject(subject);
            mimeMessageHelper.setBcc(new String[]{"18652023713@163.com"});
            mimeMessageHelper.setText(content, false);
            this.javaMailSender.send(mimeMessage);
        } catch (MessagingException e) {
            logger.error("邮件发送失败：{}", toUser);
            return true;
        } finally {
            return true;
        }
    }

    public void setFrom(String from) {
        this.from = from;
    }

    public void setToUser(String toUser) {
        this.toUser = toUser;
    }
}
